//
//  PopUpViewController.m
//  SlideMenu
//
//  Created by khoa nguyen on 7/18/14.
//  Copyright (c) 2014 Aryan Ghassemi. All rights reserved.
//

#import "NewProjectPopUpViewController.h"
#import "AppDelegate.h"
#import "HomeViewController.h"

@interface NewProjectPopUpViewController ()

@property AppDelegate* appdelegate;

@end

@implementation NewProjectPopUpViewController

@synthesize textProjectName;
@synthesize appdelegate;

-(void)updateDelegate{
    [_delegate updateUI:self];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    
    self.view.backgroundColor=[[UIColor blackColor] colorWithAlphaComponent:.6];
    self.popUpView.layer.cornerRadius = 5;
    self.popUpView.layer.shadowOpacity = 0.8;
    self.popUpView.layer.shadowOffset = CGSizeMake(0.0f, 0.0f);
    
     appdelegate = (AppDelegate *)[[UIApplication sharedApplication]delegate];
    
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)showAnimate
{
    self.view.transform = CGAffineTransformMakeScale(1.3, 1.3);
    self.view.alpha = 0;
    [UIView animateWithDuration:.25 animations:^{
        self.view.alpha = 1;
        self.view.transform = CGAffineTransformMakeScale(1, 1);
    }];
}

- (void)removeAnimate
{
    [UIView animateWithDuration:.25 animations:^{
        self.view.transform = CGAffineTransformMakeScale(1.3, 1.3);
        self.view.alpha = 0.0;
    } completion:^(BOOL finished) {
        if (finished) {
            [self.view removeFromSuperview];
        }
    }];
}

- (IBAction)closePopupCancel:(id)sender {
    [self removeAnimate];
}

-(IBAction)closePopupOK:(id)sender {

    
    bool val = [ _delegate checkProjectAvailable:textProjectName.text];
    
    if( val)
    {
        [self removeAnimate];
        return;
    }
    
    NSManagedObjectContext *context = [appdelegate managedObjectContext];
    
    NSManagedObject *failedBankInfo = [NSEntityDescription
                                       insertNewObjectForEntityForName:@"Project"
                                       inManagedObjectContext:context];
    
    [failedBankInfo setValue:textProjectName.text forKey:@"name"];
    [failedBankInfo setValue:@5 forKey:@"device_id"];
    [failedBankInfo setValue:@1 forKey:@"id"];

    NSError *error;
    if (![context save:&error]) {
        NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);
    }
    else{
         NSLog(@"Whoops, success save: ");
    }

    [self updateDelegate];
    
    [self removeAnimate];
}

- (void)showInView:(UIView *)aView withImage:(UIImage *)image withMessage:(NSString *)message animated:(BOOL)animated
{
    [aView addSubview:self.view];
    if (animated) {
        [self showAnimate];
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}




@end
